home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / blanker / source / blankers / puzzle / puzzle.c < prev    next >
C/C++ Source or Header  |  1993-07-03  |  6KB  |  240 lines

  1. /*
  2.  *  Source machine generated by GadToolsBox V2.0b
  3.  *  which is (c) Copyright 1991-1993 Jaba Development
  4.  *
  5.  *  GUI Designed by : Michael D. Bayne
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/classes.h>
  11. #include <intuition/classusr.h>
  12. #include <intuition/imageclass.h>
  13. #include <intuition/gadgetclass.h>
  14. #include <libraries/gadtools.h>
  15. #include <graphics/displayinfo.h>
  16. #include <graphics/gfxbase.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/gadtools_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/utility_protos.h>
  22. #include <string.h>
  23. #include <clib/diskfont_protos.h>
  24.  
  25. #include "Puzzle.h"
  26.  
  27. struct Screen         *Scr = NULL;
  28. UBYTE                 *PubScreenName = NULL;
  29. APTR                   VisualInfo = NULL;
  30. struct Window         *PuzzleWnd = NULL;
  31. struct Gadget         *PuzzleGList = NULL;
  32. struct IntuiMessage    PuzzleMsg;
  33. struct Gadget         *PuzzleGadgets[5];
  34. UWORD                  PuzzleLeft = 0;
  35. UWORD                  PuzzleTop = 12;
  36. UWORD                  PuzzleWidth = 237;
  37. UWORD                  PuzzleHeight = 40;
  38. UBYTE                 *PuzzleWdt = (UBYTE *)"Puzzle Prefs";
  39. struct TextAttr       *Font, Attr;
  40. UWORD                  FontX, FontY;
  41. UWORD                  OffX, OffY;
  42. struct TextFont       *PuzzleFont = NULL;
  43.  
  44. UWORD PuzzleGTypes[] = {
  45.     SLIDER_KIND,
  46.     SLIDER_KIND,
  47.     BUTTON_KIND,
  48.     BUTTON_KIND,
  49.     BUTTON_KIND
  50. };
  51.  
  52. struct NewGadget PuzzleNGad[] = {
  53.     53, 2, 154, 10, (UBYTE *)"_Horiz.", NULL, GD_HORIZ, PLACETEXT_LEFT, NULL, (APTR)HORIZClicked,
  54.     53, 14, 154, 10, (UBYTE *)"_Vert.", NULL, GD_VERT, PLACETEXT_LEFT, NULL, (APTR)VERTClicked,
  55.     9, 26, 66, 12, (UBYTE *)"_Ok", NULL, GD_OK, PLACETEXT_IN, NULL, (APTR)OKClicked,
  56.     163, 26, 66, 12, (UBYTE *)"_Cancel", NULL, GD_CANCEL, PLACETEXT_IN, NULL, (APTR)CANCELClicked,
  57.     86, 26, 66, 12, (UBYTE *)"_Test", NULL, GD_TEST, PLACETEXT_IN, NULL, (APTR)TESTClicked
  58. };
  59.  
  60. ULONG PuzzleGTags[] = {
  61.     (GTSL_Min), 2, (GTSL_Max), 14, (GTSL_Level), 7, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE),
  62.     (GTSL_Min), 2, (GTSL_Max), 10, (GTSL_Level), 5, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE),
  63.     (GT_Underscore), '_', (TAG_DONE),
  64.     (GT_Underscore), '_', (TAG_DONE),
  65.     (GT_Underscore), '_', (TAG_DONE)
  66. };
  67.  
  68. static UWORD ComputeX( UWORD value )
  69. {
  70.     return(( UWORD )((( FontX * value ) + 3 ) / 6 ));
  71. }
  72.  
  73. static UWORD ComputeY( UWORD value )
  74. {
  75.     return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  76. }
  77.  
  78. static void ComputeFont( UWORD width, UWORD height )
  79. {
  80.     Forbid();
  81.     Font = &Attr;
  82.     Font->ta_Name = (STRPTR)GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
  83.     Font->ta_YSize = FontY = GfxBase->DefaultFont->tf_YSize;
  84.     FontX = GfxBase->DefaultFont->tf_XSize;
  85.     Permit();
  86.  
  87.     OffX = Scr->WBorLeft;
  88.     OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;
  89.  
  90.     if ( width && height ) {
  91.         if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
  92.             goto UseTopaz;
  93.         if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
  94.             goto UseTopaz;
  95.     }
  96.     return;
  97.  
  98. UseTopaz:
  99.     Font->ta_Name = (STRPTR)"topaz.font";
  100.     FontX = FontY = Font->ta_YSize = 8;
  101. }
  102.  
  103. int SetupScreen( void )
  104. {
  105.     if ( ! ( Scr = LockPubScreen( PubScreenName )))
  106.         return( 1L );
  107.  
  108.     ComputeFont( 0, 0 );
  109.  
  110.     if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
  111.         return( 2L );
  112.  
  113.     return( 0L );
  114. }
  115.  
  116. void CloseDownScreen( void )
  117. {
  118.     if ( VisualInfo ) {
  119.         FreeVisualInfo( VisualInfo );
  120.         VisualInfo = NULL;
  121.     }
  122.  
  123.     if ( Scr        ) {
  124.         UnlockPubScreen( NULL, Scr );
  125.         Scr = NULL;
  126.     }
  127. }
  128.  
  129. int HandlePuzzleIDCMP( void )
  130. {
  131.     struct IntuiMessage    *m;
  132.     int            (*func)();
  133.     BOOL            running = TRUE;
  134.  
  135.     while( m = GT_GetIMsg( PuzzleWnd->UserPort )) {
  136.  
  137.         CopyMem(( char * )m, ( char * )&PuzzleMsg, (long)sizeof( struct IntuiMessage ));
  138.  
  139.         GT_ReplyIMsg( m );
  140.  
  141.         switch ( PuzzleMsg.Class ) {
  142.  
  143.             case    IDCMP_REFRESHWINDOW:
  144.                 GT_BeginRefresh( PuzzleWnd );
  145.                 GT_EndRefresh( PuzzleWnd, TRUE );
  146.                 break;
  147.  
  148.             case    IDCMP_VANILLAKEY:
  149.                 running = PuzzleVanillaKey();
  150.                 break;
  151.  
  152.             case    IDCMP_GADGETUP:
  153.             case    IDCMP_GADGETDOWN:
  154.                 func = ( void * )(( struct Gadget * )PuzzleMsg.IAddress )->UserData;
  155.                 running = func();
  156.                 break;
  157.         }
  158.     }
  159.     return( running );
  160. }
  161.  
  162. int OpenPuzzleWindow( void )
  163. {
  164.     struct NewGadget    ng;
  165.     struct Gadget    *g;
  166.     UWORD        lc, tc;
  167.     UWORD        wleft = PuzzleLeft, wtop = PuzzleTop, ww, wh;
  168.  
  169.     ComputeFont( PuzzleWidth, PuzzleHeight );
  170.  
  171.     ww = ComputeX( PuzzleWidth );
  172.     wh = ComputeY( PuzzleHeight );
  173.  
  174.     wleft = ( Scr->Width - ww )/2 - Scr->WBorRight;
  175.     wtop = ( Scr->Height - wh - Scr->WBorBottom*2 - Font->ta_YSize )/2;
  176.  
  177.     if ( ! ( PuzzleFont = OpenDiskFont( Font )))
  178.         return( 5L );
  179.  
  180.     if ( ! ( g = CreateContext( &PuzzleGList )))
  181.         return( 1L );
  182.  
  183.     for( lc = 0, tc = 0; lc < Puzzle_CNT; lc++ ) {
  184.  
  185.         CopyMem((char * )&PuzzleNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
  186.  
  187.         ng.ng_VisualInfo = VisualInfo;
  188.         ng.ng_TextAttr   = Font;
  189.         ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
  190.         ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
  191.         ng.ng_Width      = ComputeX( ng.ng_Width );
  192.         ng.ng_Height     = ComputeY( ng.ng_Height);
  193.  
  194.         PuzzleGadgets[ lc ] = g = CreateGadgetA((ULONG)PuzzleGTypes[ lc ], g, &ng, ( struct TagItem * )&PuzzleGTags[ tc ] );
  195.  
  196.         while( PuzzleGTags[ tc ] ) tc += 2;
  197.         tc++;
  198.  
  199.         if ( NOT g )
  200.             return( 2L );
  201.     }
  202.  
  203.     if ( ! ( PuzzleWnd = OpenWindowTags( NULL,
  204.                 WA_Left,    wleft,
  205.                 WA_Top,        wtop,
  206.                 WA_Width,    ww + OffX + Scr->WBorRight,
  207.                 WA_Height,    wh + OffY + Scr->WBorBottom,
  208.                 WA_IDCMP,    SLIDERIDCMP|BUTTONIDCMP|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
  209.                 WA_Flags,    WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP,
  210.                 WA_Gadgets,    PuzzleGList,
  211.                 WA_Title,    PuzzleWdt,
  212.                 WA_ScreenTitle,    "Puzzle Prefs",
  213.                 WA_PubScreen,    Scr,
  214.                 TAG_DONE )))
  215.     return( 4L );
  216.  
  217.     GT_RefreshWindow( PuzzleWnd, NULL );
  218.  
  219.     return( 0L );
  220. }
  221.  
  222. void ClosePuzzleWindow( void )
  223. {
  224.     if ( PuzzleWnd        ) {
  225.         CloseWindow( PuzzleWnd );
  226.         PuzzleWnd = NULL;
  227.     }
  228.  
  229.     if ( PuzzleGList      ) {
  230.         FreeGadgets( PuzzleGList );
  231.         PuzzleGList = NULL;
  232.     }
  233.  
  234.     if ( PuzzleFont ) {
  235.         CloseFont( PuzzleFont );
  236.         PuzzleFont = NULL;
  237.     }
  238. }
  239.  
  240.